﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using Common.DBOper;
using Common;
using VibrationTest.OffLineChart;

namespace VibrationTest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        string selectitemName = "";

        // 拖动的源数据行索引
        private int indexOfItemUnderMouseToDrag = -1;
  
        private Rectangle dragBoxFromMouseDown = Rectangle.Empty;
        OperateData operdata = new OperateData();
        OperateTest opertest = new OperateTest();

        OperateSection opertesection = new OperateSection();

        OperateChannel operchannel = new OperateChannel();
        OperationText opertext = new OperationText();
        public static Dictionary<string, frmLineChart_offline> frmlinechart = new Dictionary<string, frmLineChart_offline>();

       public static Dictionary<string, frmHistogram_offline> frmHistorgramchart = new Dictionary<string, frmHistogram_offline>();
        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                TreeView tv = sender as TreeView;
                TreeNode select = tv.GetNodeAt(e.Location);
                if (select != null)
                {
                    //如果选择节点不是null，就设置为你的“选取结点
                    tv.SelectedNode = select;
                }


            }
            catch (Exception ex)
            {
                //异常处理
            }
            if (e.Button == MouseButtons.Right && e.Clicks == 1) 
            {
                   this.treeView1.ContextMenuStrip = this.contextMenuStrip1;
                   if (treeView1.SelectedNode.Level == 0)
                   {
                       this.contextMenuStrip1.Items[0].Visible = true;
                       this.contextMenuStrip1.Items[1].Visible = false;
                       this.contextMenuStrip1.Items[2].Visible = false;
                       this.contextMenuStrip1.Items[3].Visible = false;
                       this.contextMenuStrip1.Items[4].Visible = false;
                   }
                   if (treeView1.SelectedNode.Level == 1)
                   {
                       this.contextMenuStrip1.Items[0].Visible = false;
                       this.contextMenuStrip1.Items[1].Visible = true;
                       this.contextMenuStrip1.Items[2].Visible = true;
                       this.contextMenuStrip1.Items[3].Visible = false;
                       this.contextMenuStrip1.Items[4].Visible = true;
                   }
                   if (treeView1.SelectedNode.Level == 2)
                   {
                       this.contextMenuStrip1.Items[0].Visible = false;
                       this.contextMenuStrip1.Items[1].Visible = false;
                       this.contextMenuStrip1.Items[2].Visible = false;
                       this.contextMenuStrip1.Items[3].Visible = true;
                       this.contextMenuStrip1.Items[4].Visible = true;
                   }
            }

            selectitemName = treeView1.SelectedNode.Text;
        }

        private void 添加项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddProject();
        }


        public void AddProject() 
        {
            try
            {


                if (this.treeView1.Nodes[0].Nodes.Count>0)
                {
                    MessageBox.Show("已经存在一个Project，不能添加","提示");
                    return;
                }
                //int projectcount = this.treeView1.SelectedNode.Nodes.Count+1;
                string ProjectName = "Project" + DateTime.Now.ToString("MMddHHmmss");
                this.treeView1.SelectedNode.Nodes.Add(ProjectName);

                string iniFilePath = System.Windows.Forms.Application.StartupPath + "\\DB.mdb";
                string newFilePath = System.Windows.Forms.Application.StartupPath + "\\Test\\" + ProjectName + ".mdb";
                string FileNameNew = System.Windows.Forms.Application.StartupPath + "\\Test\\" + ProjectName;
                File.Copy(iniFilePath, newFilePath);
                string currPath = Application.StartupPath;
                //检查是否存在文件夹


                if (false == System.IO.Directory.Exists(FileNameNew))
                {
                    //创建pic文件夹
                    System.IO.Directory.CreateDirectory(FileNameNew);
                }
                if (Mdl_Class.testTopath.ContainsKey(ProjectName))
                {
                    Mdl_Class.testTopath[ProjectName] = newFilePath;
                }
                else
                {
                    Mdl_Class.testTopath.Add(ProjectName, newFilePath);
                }
                Mdl_Class.FilePath = newFilePath;

                this.textBox1.Text = Mdl_Class.FilePath;
                Program.frmmain.AddSection();
                this.treeView1.ExpandAll();
            }
            catch (Exception ex)
            {

                MessageBox.Show("添加项目失败", "提示");
            }
        }

        private void 删除项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.frmmain.ClosePrj();



            
        }

        public void ClosePrj() 
        {
            if (this.treeView1.Nodes[0].Nodes.Count>0) {
                if (Mdl_Class.IsQuitProgram ==false) 
                {
                    MessageBox.Show("正在采集数据中，不能关闭Project","提示");
                    return;
                }
                if (MessageBox.Show("确定要关闭Project吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
           
                    if (Mdl_Class.testTopath.ContainsKey(this.treeView1.Nodes[0].Nodes[0].Text))
                    {
                        Mdl_Class.testTopath.Remove(this.treeView1.Nodes[0].Nodes[0].Text);
                    }
                    this.treeView1.Nodes[0].Nodes[0].Remove();
                    Mdl_Class.FilePath = "";
                    Mdl_Class.NowStectionId = 0;
                    Program.form1.ClearSection();
                    this.textBox1.Text = "";
                }
            }
        }

        private void 添加子测试项ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            AddSection();
        }

        public void AddSection() 
        {
            int projectcount = 1;

            for (int i = 0; i < this.treeView1.Nodes[0].Nodes[0].Nodes.Count; i++)
            {
                if (this.treeView1.Nodes[0].Nodes[0].Nodes[i].Text != "section" + projectcount.ToString())
                {

                    break;
                }
                else
                {
                    projectcount++;
                }
            }

            if (opertesection.AddSection("section" + projectcount.ToString()) > 0)
            {
                this.treeView1.Nodes[0].Nodes[0].Nodes.Add("section" + projectcount.ToString());
                this.treeView1.Nodes[0].Nodes[0].Nodes[this.treeView1.Nodes[0].Nodes[0].Nodes.Count - 1].ImageIndex = 2;
                this.treeView1.Nodes[0].Nodes[0].Nodes[this.treeView1.Nodes[0].Nodes[0].Nodes.Count - 1].SelectedImageIndex = 2;

                Program.form1.DisplaySection();
            }
            else
            {
                MessageBox.Show("添加section失败", "提示");
            }
        }

        private void 删除子项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
        
            if (MessageBox.Show("确定要删除section吗？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes){

                if (Mdl_Class.IsQuitProgram == false&& Mdl_Class.NowStectionName== this.treeView1.SelectedNode.Text)
                {
                    MessageBox.Show("正在采集数据中，不能删除", "提示");
                    return;
                }
                if (opertesection.DeleteSectionBySectionName(this.treeView1.SelectedNode.Text) > 0)
                {
                    this.treeView1.SelectedNode.Remove();
                    Program.form1.DisplaySection();
                }
                else 
                {
                    MessageBox.Show("删除失败","提示");
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            if (treeView1.SelectedNode.Level == 4)
            {

                //if (treeView1.SelectedNode.Index == 0)
                //{
                //      this.dataGridView1.Rows.Add("测点1");
                //        this.dataGridView1.Rows.Add("测点4");
                //        this.dataGridView1.Rows.Add("测点5");
                //}
                //else
                //{
                //    this.dataGridView1.Rows.Add("测点2");
                //    this.dataGridView1.Rows.Add("测点3");
                //    this.dataGridView1.Rows.Add("测点8");
                //    this.dataGridView1.Rows.Add("测点9");
                //    this.dataGridView1.Rows.Add("测点10");
                //}
                this.dataGridView1.Rows.Clear();
                try
                {

            
                string sectionname = treeView1.SelectedNode.Parent.Parent.Text;
                Cls_Section clssection = opertesection.GetSectionListBySectionName(sectionname);
                List<Common.DBOper.Cls_Channel> channelist = operchannel.GetChannelList(clssection.SectionId);
                Cls_Test clstest= opertest.GetTestByTestName(treeView1.SelectedNode.Parent.Text, clssection.SectionId);
                string chartname= treeView1.SelectedNode.Text;
                if (channelist != null && channelist.Count > 0)
                {
                    for (int i = 0; i < channelist.Count; i++)
                    {
                        if (channelist[i].IsUse == (byte)1)
                        {
                            this.dataGridView1.Rows.Add(channelist[i].ChannelId, clstest.TestId, chartname + " " + channelist[i].TestPointName, sectionname, clstest.TestName, channelist[i].TestPointName);
                        }
                    }
                }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("加载通道信息异常，异常信息:"+ex.ToString(),"提示");
                }
            }
        
        }

        private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeView1.SelectedNode.BeginEdit();

      
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            TreeNode renode = e.Node;
            if (renode.Parent==null)
            {
                return;
            }
            //if (treeView1.SelectedNode.Level != 1)
            //{
            //    e.CancelEdit = true;
            //    return;
            //}
            foreach (TreeNode item in renode.Parent.Nodes)
            {
                if (item.Text==e.Label&&item.Index!=e.Node.Index)
                {
                    MessageBox.Show("名为“" + e.Label + "”的项已存在，请为您要添加的项提供一个唯一的名称");
                    e.CancelEdit = true;
                    return;
                }
            }
            //e.CancelEdit = true;

            if (treeView1.SelectedNode.Level == 1)
            {
                if (e==null||e.Label==null)
                {
                    return;
                }
                string iniFilePath = System.Windows.Forms.Application.StartupPath + "\\Test\\" + selectitemName+".mdb";
                string newFilePath = System.Windows.Forms.Application.StartupPath + "\\Test\\" + e.Label + ".mdb";

                string oldFile= System.Windows.Forms.Application.StartupPath + "\\Test\\" + selectitemName;
                string newFile = System.Windows.Forms.Application.StartupPath + "\\Test\\" + e.Label;

                if (Mdl_Class.testTopath.ContainsKey(selectitemName))
                {
                    Mdl_Class.testTopath.Remove(selectitemName);
                }

                if (Mdl_Class.testTopath.ContainsKey(e.Label))
                {
                    Mdl_Class.testTopath[e.Label] = newFilePath;
                }
                else 
                {
                    Mdl_Class.testTopath.Add(e.Label, newFilePath);
                }
                Mdl_Class.FilePath = newFilePath;
                File.Move(iniFilePath, newFilePath);
                Directory.Move(oldFile, newFile);

                this.textBox1.Text = Mdl_Class.FilePath;
            }
            else if (treeView1.SelectedNode.Level == 2)
            {
                opertesection.UpdateSection_Name(selectitemName, e.Label);
                   Program.form1.DisplaySection();
            }
            else if (treeView1.SelectedNode.Level == 3)
            {

            }
            else if (treeView1.SelectedNode.Level == 4)
            {

            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = 0;
        }
        private DataGridViewSelectedRowCollection sourceRowCollection = null;
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            //捕获鼠标点击区域的信息
          
            DataGridView.HitTestInfo hitTestInfo = this.dataGridView1.HitTest(e.X, e.Y);
            indexOfItemUnderMouseToDrag = hitTestInfo.RowIndex;
            if (hitTestInfo.RowIndex < 0)
            {
                return;
            }
            this.dataGridView1.Rows[hitTestInfo.RowIndex].Selected = true;
            if ( hitTestInfo.RowIndex > -1)
            {
                if (this.dataGridView1.SelectedRows.Count > 0)
                {
                    sourceRowCollection = this.dataGridView1.SelectedRows;
                }
            }
            else
                sourceRowCollection = null;

        }

        private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            // 释放鼠标按键时清空变量为默认值
            //dragBoxFromMouseDown = Rectangle.Empty;
        }

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
           if (e.Button == MouseButtons.Left)
            {
                if (sourceRowCollection != null)
                {
                    DragDropEffects effect = this.dataGridView1.DoDragDrop(sourceRowCollection, DragDropEffects.Move);
                    if (effect == DragDropEffects.Move)
                    {
                        //将sourceRowCollection重新置空
                        sourceRowCollection = null;
                    }
                }
            }
        }

        private void chart1_DragOver(object sender, DragEventArgs e)
        {
             if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
            {

                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;  //这个值会返回给DoDragDrop方法
            }

        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            Program.frmmain = this;
            //this.cmb_type.SelectedIndex=0;
        }


        public void LoadFile(string prjName) 
        {
            for (int i = 0; i < this.treeView1.Nodes[0].Nodes.Count; i++)
            {
                if (this.treeView1.Nodes[0].Nodes[i].Text == prjName)
                {
                    MessageBox.Show("项目"+prjName+"已经被打开","提示");
                    if (Mdl_Class.testTopath.ContainsKey(prjName))
                    {
                        Mdl_Class.testTopath.Remove(prjName);
                    }
                    return;
                }
            }
            Mdl_Class.testNameTotest.Clear();
            this.treeView1.Nodes[0].Nodes.Clear();
            this.treeView1.Nodes[0].Nodes.Add(prjName);
            this.textBox1.Text = Mdl_Class.FilePath;
            if (Mdl_Class.sectionNameTosectionId != null)
            {

               foreach (var item in Mdl_Class.sectionNameTosectionId.Keys)
	            {



                    this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Add(item);
                    this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes[this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1].ImageIndex = 2;
                    this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes[this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1].SelectedImageIndex = 2;
                    int index1=this.treeView1.Nodes.Count - 1;
                   int index2=this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1;
                    List<Cls_Test> clstestlist = opertest.GetTestList(Mdl_Class.sectionNameTosectionId[item]);
                    if (clstestlist != null && clstestlist.Count>0)
                   {
                       for (int i = 0; i < clstestlist.Count; i++)
                       {
                           Mdl_Class.testNameTotest.Add(clstestlist[i].TestName,clstestlist[i]);
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes.Add(clstestlist[i].TestName);
                           int index3=this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes.Count-1;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].ImageIndex = 6;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].SelectedImageIndex = 6;



                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("频谱数据");
                           int index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("1/3倍频程");
                            index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("时域数据");
                            index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("原始数据");
                            index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                           this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;
                       }
                   }
                }
            }

            this.treeView1.ExpandAll();
        }


        public void ReflashTestName() 
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                Mdl_Class.testNameTotest.Clear();
                this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Clear();
                if (Mdl_Class.sectionNameTosectionId != null)
                {

                    foreach (var item in Mdl_Class.sectionNameTosectionId.Keys)
                    {



                        this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Add(item);
                        this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes[this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1].ImageIndex = 2;
                        this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes[this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1].SelectedImageIndex = 2;
                        int index1 = this.treeView1.Nodes.Count - 1;
                        int index2 = this.treeView1.Nodes[0].Nodes[this.treeView1.Nodes.Count - 1].Nodes.Count - 1;
                        List<Cls_Test> clstestlist = opertest.GetTestList(Mdl_Class.sectionNameTosectionId[item]);
                        if (clstestlist != null && clstestlist.Count > 0)
                        {
                            for (int i = 0; i < clstestlist.Count; i++)
                            {
                                Mdl_Class.testNameTotest.Add(clstestlist[i].TestName, clstestlist[i]);
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes.Add(clstestlist[i].TestName);
                                int index3 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes.Count - 1;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].ImageIndex = 6;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].SelectedImageIndex = 6;



                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("频谱数据");
                                int index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("1/3倍频程");
                                index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("时域数据");
                                index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;

                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Add("原始数据");
                                index4 = this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes.Count - 1;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].ImageIndex = 5;
                                this.treeView1.Nodes[0].Nodes[index1].Nodes[index2].Nodes[index3].Nodes[index4].SelectedImageIndex = 5;
                            }
                        }
                    }
                }

               
            }));
        }

        private void chart1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.toolTip1.Hide(this);
            }
        }
        List<int> linechartcount = new List<int>();
        private void bt_lineChart_Click(object sender, EventArgs e)
        {


            AddZX_Chart();

        }

        public void AddZX_Chart() 
        {
            int chartNum = 1;
            List<int> chartindexlist = new List<int>();
            for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
            {
                if (this.tabControl1.TabPages[i].Text.Contains("折线图"))
                {
                    string[] linetitle = this.tabControl1.TabPages[i].Text.Split('_');
                    int chartindex = Convert.ToInt32(linetitle[1]);

                    chartindexlist.Add(chartindex);
                }
            }

            for (int j = 1; j <= chartindexlist.Count + 1; j++)
            {
                if (!chartindexlist.Contains(j))
                {
                    chartNum = j;
                }
            }
            this.tabControl1.TabPages.Add("折线图_" + chartNum.ToString());

            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Clear();
            frmLineChart_offline frmlineoff = new frmLineChart_offline();
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name = "折线图_" + chartNum.ToString();
            if (frmlinechart.ContainsKey(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name)) 
            {
                frmlinechart.Remove(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name);
            }
            frmlinechart.Add(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name, frmlineoff);
            frmlineoff.TopLevel = false;
            frmlineoff.Size = this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Size;
            frmlineoff.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Add(frmlineoff);
            frmlineoff.Show();
        }

        private void bt_Histogram_Click(object sender, EventArgs e)
        {
            AddZZ_Chart();
        }

        public void AddZZ_Chart() 
        {
            int chartNum = 1;
            List<int> chartindexlist = new List<int>();
            for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
            {
                if (this.tabControl1.TabPages[i].Text.Contains("柱状图"))
                {
                    string[] linetitle = this.tabControl1.TabPages[i].Text.Split('_');
                    int chartindex = Convert.ToInt32(linetitle[1]);

                    chartindexlist.Add(chartindex);
                }
            }

            for (int j = 1; j <= chartindexlist.Count + 1; j++)
            {
                if (!chartindexlist.Contains(j))
                {
                    chartNum = j;
                }
            }
            this.tabControl1.TabPages.Add("柱状图_" + chartNum.ToString());
            
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Clear();
            frmHistogram_offline frmhistorgramoff = new frmHistogram_offline();
            frmhistorgramoff.TopLevel = false;
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name = "柱状图_" + chartNum.ToString();

            if (frmHistorgramchart.ContainsKey(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name))
            {
                frmHistorgramchart.Remove(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name);
            }
            frmHistorgramchart.Add(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name, frmhistorgramoff);
            frmhistorgramoff.Size = this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Size;
            frmhistorgramoff.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Add(frmhistorgramoff);
            frmhistorgramoff.Show();
        }

        /// <summary>
        /// 倍频程多折线图
        /// </summary>
        public void AddBPC_Chart()
        {
            int chartNum = 1;
            List<int> chartindexlist = new List<int>();
            for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
            {
                if (this.tabControl1.TabPages[i].Text.Contains("倍频程多折线图"))
                {
                    string[] linetitle = this.tabControl1.TabPages[i].Text.Split('_');
                    int chartindex = Convert.ToInt32(linetitle[1]);

                    chartindexlist.Add(chartindex);
                }
            }

            for (int j = 1; j <= chartindexlist.Count + 1; j++)
            {
                if (!chartindexlist.Contains(j))
                {
                    chartNum = j;
                }
            }
            this.tabControl1.TabPages.Add("倍频程多折线图_" + chartNum.ToString());

            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Clear();
            frmLineChart_offline frmlineoff = new frmLineChart_offline();
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name = "倍频程多折线图_" + chartNum.ToString();
            frmlinechart.Add(this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Name, frmlineoff);
            frmlineoff.TopLevel = false;
            frmlineoff.chart_type = "1/3倍频程";
            frmlineoff.Size = this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Size;
            frmlineoff.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.TabPages[this.tabControl1.TabPages.Count - 1].Controls.Add(frmlineoff);
            frmlineoff.Show();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tabpage = this.tabControl1.SelectedTab;
            if (frmlinechart.ContainsKey(tabpage.Name))
            {
                frmlinechart[tabpage.Name].RemoveToolTip();
                frmlinechart.Remove(tabpage.Name);
            }

            if (frmHistorgramchart.ContainsKey(tabpage.Name))
            {
                frmHistorgramchart[tabpage.Name].RemoveToolTip();
                frmHistorgramchart.Remove(tabpage.Name);
            }
            this.tabControl1.TabPages.Remove(tabpage);

        }

        private void 除此之外全部删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tabpage = this.tabControl1.SelectedTab;
            for (int i = 0; i <   this.tabControl1.TabPages.Count; i++)
            {
               TabPage tabpage_new=this.tabControl1.TabPages[i];
               if (tabpage_new != tabpage)
                {
                    this.tabControl1.TabPages.Remove(tabpage_new);
                    i--;
                }
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var item in frmlinechart.Keys)
            {
                frmlinechart[item].RemoveToolTip();
            }

            foreach (var item in frmHistorgramchart.Keys)
            {
                frmHistorgramchart[item].RemoveToolTip();
            }
        }

        private void 导出数据TXTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {


                if (this.dataGridView1.SelectedRows.Count > 0)
                {
                    string channelid = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    string testid = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    string channelname = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                    Cls_Data clsdatalist = operdata.GetDataByChannelid(testid, Convert.ToByte(channelid));
                    if (clsdatalist != null)
                    {
                        System.Windows.Forms.SaveFileDialog objSave = new System.Windows.Forms.SaveFileDialog();
                        objSave.Filter = "(*.txt)|*.txt|" + "(*.*)|*.*";
                        if (objSave.ShowDialog() == DialogResult.OK)
                        {
                            string filename = objSave.FileName;
                            using (StreamWriter objWriter = new StreamWriter(filename, false, System.Text.Encoding.UTF8))
                            {
                                string result = "";

                                if (channelname.Contains("频谱数据"))
                                {
                                    result = String.Join(",", clsdatalist.AccelerationFFTAmpDB.ToArray());
                                    objWriter.WriteLine(result);
                                }
                                else if (channelname.Contains("1/3倍频程"))
                                {
                                    result = String.Join(",", clsdatalist._1_3octavedB.ToArray());
                                    objWriter.WriteLine(result);
                                }
                                else if (channelname.Contains("时域数据"))
                                {
                                    result = String.Join(",", clsdatalist.AccelerationTimewave.ToArray());
                                    objWriter.WriteLine(result);
                                }
                                else if (channelname.Contains("原始数据"))
                                {
                                    string nowsectionName = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                                    string nowtestName = this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                                    string nowchannel = this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                                    string filePath = Mdl_Class.FilePath.Split('.')[0] + "\\" + nowsectionName + "\\" + nowtestName;
                                    string fileName = "原始数据_" + nowchannel + ".ini";
                                    List<float> displayvalue = opertext.ReadOldData(filePath + "\\" + fileName);
                                    result = String.Join(",", displayvalue.ToArray());
                                    objWriter.WriteLine(result);
                                }
                                else
                                {
                                    MessageBox.Show("不支持该数据类型", "提示");
                                    return;
                                }
                            }
                            MessageBox.Show("导出成功", "提示");
                        }
                    }
                    else
                    {
                        MessageBox.Show("未查询可导出的数据", "提示");
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("导出异常，异常信息：" + ex.Message, "提示");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddBPC_Chart();
        }
    }
}
